#!/usr/bin/python
# encoding=utf-8
'''
pip install PyMySQL

https://pymysql.readthedocs.io/en/latest/
'''
import time
import pymysql

connection = pymysql.connect(host='39.104.14.106',
                             user='my-all-in',
                             password='abcd4321',
                             db='my-all-in',
                             charset='utf8mb4')

try:
    while True:
        with connection.cursor() as cursor:
            cursor.execute('''
                select count(1) from t_job_task where status in ('ING', 'NEW')
            ''')
            qty = cursor.fetchone()[0]
            print('qty: ' + str(qty))
            if qty <= 3:
                cursor.execute(
                    '''
                        insert into t_job_task(type, biz, status, done)
                        select
                            'ZDN_NEW_REGISTER', concat('{"promotionMobile": "', mobile, '"}'), 'NEW', 0
                        from
                            t_zdn_sub_account
                        where
                            idStatus = '实名认证成功'
                        and mobile not in (select promotionMobile from t_zdn_sub_account)
                        order by registerTime desc
                        limit 50
                    '''
                )
        time.sleep(5)
    connection.commit()
finally:
    connection.close()
